package com.robusta.cityuate.services;

import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Set;

import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ByteArrayEntity;
import org.json.JSONException;
import org.json.JSONObject;

import com.robusta.cityuate.utils.ConnectionUtils;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class RegistrationService extends HTTPService {

	private final String REQUEST_URL = "/registrations";
	private final Context mContext = this;
	private final String USER = "user";
	private final String TAG = RegistrationService.class.getName();

	@Override
	protected void onResponse() {
		Intent loginUIupdate = new Intent(assignedFilter);
		Log.d(TAG, "AssignedFilter= " + assignedFilter);
		switch (responseStatusCode) {
		case HttpStatus.SC_CREATED: {// successful login
			// remove connection from shared pref
			ConnectionUtils.setDisconnecting(mContext, true, assignedFilter,
					null);
			// update UI if active
			loginUIupdate.putExtra(RESUlT, true);
			// Log.d(TAG, "Success Token " + token);
			break;
		}
		case HttpStatus.SC_UNPROCESSABLE_ENTITY: {
			// extract the message
			JSONObject responseMsg;
			String msg = "";
			try {
				responseMsg = new JSONObject(responseString);
				for (@SuppressWarnings("unchecked")
				Iterator<String> iterator = responseMsg.keys(); iterator
						.hasNext();) {
					String key = (String) iterator.next();
					msg +=(key+" "+ responseMsg.getString(key) + "\n");
				}
				Log.e(TAG, "msg " + msg);
			} catch (JSONException e) {
				e.printStackTrace();

			}
			// remove connection from shared pref
			ConnectionUtils.setDisconnecting(mContext, false, assignedFilter,
					msg);
			// update UI if active
			loginUIupdate.putExtra(RESUlT, false);
			Log.d(TAG, "RegistrationFailed" + msg);
		}

		}
		sendOrderedBroadcast(loginUIupdate, null);
	}

	@Override
	protected void onNoResponse(String msg) {
		// remove connection from shared pref keeping result and reason
		ConnectionUtils.setDisconnecting(mContext, false, assignedFilter,
				msg);
		// update UI if active
		Intent loginUIupdate = new Intent(assignedFilter);
		loginUIupdate.putExtra(RESUlT, false);
		sendOrderedBroadcast(loginUIupdate, null);

	}

	@Override
	protected HttpRequestBase initRequest() {
		HttpPost req = new HttpPost(API_URL + REQUEST_URL);
		try {
			req.setEntity(createPostBody());
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return req;
	}

	/**
	 * creates the body of the request from parameters
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	private ByteArrayEntity createPostBody()
			throws UnsupportedEncodingException {
		JSONObject usr = new JSONObject();
		// Creating Body
		JSONObject bodyData = new JSONObject();
		if (extras != null) {

			Set<String> extrasKeys = extras.keySet();
			if (!extrasKeys.isEmpty()) {

				for (String key : extrasKeys)
					try {
						bodyData.put(key, extras.get(key).toString());
					} catch (JSONException e) {
						e.printStackTrace();
					}
			}
		}
		try {
			usr.put(USER, bodyData);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		Log.e(TAG, usr.toString());
		return new ByteArrayEntity(usr.toString().getBytes("UTF-8"));
	}
}
